package com.usabb.service.impl;

import com.ifast.common.component.oss.support.UploadServer;
import com.ifast.common.utils.Result;
import com.usabb.domain.AttachDO;
import com.usabb.service.FileUploadService;
import com.usabb.utils.UploadFileNameUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Objects;

@Slf4j
@Service
//@AllArgsConstructor
public class FileUploadServiceImpl implements FileUploadService {

    @Autowired
    private UploadServer uploader;

    @Override
    public Result<AttachDO> uploadImageOfCenter(Long baseId, String enumName, MultipartFile file) {
        if (Objects.isNull(file) || file.getSize() <= 0) {
            log.error("上传文件为空, 请重新上传!");
            return Result.fail("请上传文件！");
        }

        final String originalFilename = file.getOriginalFilename();
        final String fileName = UploadFileNameUtils.getFileName(originalFilename, "center" + baseId);

        String url = null;
        try {
            url = uploader.upload(file.getBytes(), fileName);
        } catch (IOException e) {
            log.error("upload file error. baseId:{}, file: {}", baseId, originalFilename, e);
            return Result.fail("upload file error");
        }

        AttachDO attachDO = AttachDO.ofCenter(baseId, url);
        return Result.ok(attachDO);
    }

/*    @Override
    public Result uploadImageOfCenter(Long baseId, String enumName, List<MultipartFile> files) {
        return Result.ok();
    }*/
}
